#include <stdio.h>
#include <string.h>
#include <time.h>
#include "sqlite3.h"
#define DBNAME "test.db"
int main()
{
    int i = 0;
    int rc = 0;
    char szSQL[1024];
    char szName[50], szDate[50];
    char *zErrMsg;
    sqlite3 *pdb;
    sqlite3_stmt *pstmt;
    // 打开数据库
    rc = sqlite3_open(DBNAME, &pdb);
    if (rc != SQLITE_OK)
    {   
        sqlite3_close(pdb);
        printf("无法打开数据库%s\n", DBNAME);
    }   
#if 1
    //  建表    
//  rc = sqlite3_exec(pdb,"create table user(id int,age int, name Text, birthday Text)",0,0,&zErrMsg); 
    rc = sqlite3_exec(pdb,"create table T_SERVICE_SWITCH"
                          "(" 
                             "flow_id            int,"
                             "phone_number       text,"
                             "phone_tail         text,"
                             "service_id         int,"
                             "service_identifier int,"
                             "msg_flag           int,"
                             "service_state      int,"
                             "period_identify    int,"
                             "time_section       text,"
                             "start_time         text,"
                             "end_time           text,"
                             "instance_id        int"
                           ")", 0, 0, &zErrMsg);
    if (rc != SQLITE_OK)
    {
        sqlite3_close(pdb);
        printf("无法创建表%s\n", DBNAME);
    }
    else
    {
        printf("创建表成功%s\n", DBNAME);
    }

    //  创建索引
#if 1
    char cl_create_idx[512] = {0};
    snprintf(cl_create_idx, 512, "%s", "CREATE INDEX idx_phone_number ON T_SERVICE_SWITCH ( phone_number );");
    rc = sqlite3_exec(pdb, cl_create_idx, NULL, NULL, NULL);

    if( rc != SQLITE_OK )
    {
         printf("创建索引失败: %s\n", sqlite3_errmsg(pdb));
         sqlite3_close(pdb);
         return -1;
    }
    else
    {
         printf("创建索引成功: %s\n", sqlite3_errmsg(pdb));
    }
#endif

    time_t t1, t2;

    time(&t1);

    // 开启事务
    rc = sqlite3_exec(pdb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
    if( rc != SQLITE_OK )
    {
         printf("BEGIN TRANSACTION: %s\n", sqlite3_errmsg(pdb));
         sqlite3_close(pdb);
         return -1;
    }

    // 使用绑定变量插入100万条数据
    snprintf(szSQL,1024,"%s", "INSERT INTO T_SERVICE_SWITCH "
                              "VALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12);");

    // 预编译SQL
    rc = sqlite3_prepare(pdb, szSQL, strlen(szSQL), &pstmt, NULL);

    if (rc != SQLITE_OK)
    {
        printf("sqlite3_prepare: %s\n", sqlite3_errmsg(pdb));
        sqlite3_finalize(pstmt);
        sqlite3_close(pdb);
        return -1;
    }
    else
    {
        printf("sqlite3_prepare: %s\n", sqlite3_errmsg(pdb));
    }


    for (i = 0; i <2000000; i++)
    {
        char szPhone[21];
        snprintf(szPhone, 21, "%d", i);
        sqlite3_bind_int(pstmt, 1, i);
        sqlite3_bind_text(pstmt, 2, szPhone, strlen(szPhone), NULL);
        sqlite3_bind_text(pstmt, 3, "00", 2, NULL);
        sqlite3_bind_int(pstmt, 4, 1);
        sqlite3_bind_int(pstmt, 5, 1);
        sqlite3_bind_int(pstmt, 6, 1);
        sqlite3_bind_int(pstmt, 7, 1);
        sqlite3_bind_int(pstmt, 8, 1);
        sqlite3_bind_text(pstmt, 9, "1", 1, NULL);
        sqlite3_bind_text(pstmt, 10, "00:00", 5, NULL);
        sqlite3_bind_text(pstmt, 11, "24:00", 5, NULL);
        sqlite3_bind_int(pstmt, 12, -1);

       rc = sqlite3_step(pstmt);
       if( (rc != SQLITE_OK) && (rc != SQLITE_DONE) && (rc != SQLITE_ROW) )
       {
           printf("sqlite3_step(%d): %s\n", rc, sqlite3_errmsg(pdb));
           return -1;
       }
       rc = sqlite3_reset(pstmt);
       if( rc != SQLITE_OK )
       {
           printf("sqlite3_reset(%d): %s\n", rc, sqlite3_errmsg(pdb));
           return -1;
       }
    }
    rc = sqlite3_exec(pdb, "COMMIT TRANSACTION;", NULL, NULL, NULL);

    time(&t2);
    printf("耗时： %ld 秒\n", t2 - t1);
     // 关闭数据库
#endif
    sqlite3_finalize(pstmt);
    sqlite3_close(pdb);
    return 0;
}

